package com.study.scapp.info.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study.scapp.info.common.CommonResult;
import com.study.scapp.info.pojo.InfoCommodity;
import com.study.scapp.info.service.InfoCommodityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 1
 * @data 2023/5/14
 * 商品主表
 */
@RestController
@RequestMapping("/commodity")
public class InfoCommodityController {

    @Resource
    private InfoCommodityService infoCommodityService;

    /**
     * 新增
     * @param commodityName
     * @return
     */
    @PostMapping("/add")
    public CommonResult add(@RequestParam Map<String,Object> commodityName ){
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(commodityName);
        InfoCommodity infoCommodity = jsonObject.toJavaObject(InfoCommodity.class);
        return CommonResult.success(infoCommodityService.save(infoCommodity));
    }

    /**
     * 列表
     * @return
     */
    @GetMapping("/list")
    public CommonResult list(String name){
        List<InfoCommodity> list;
        if ("全部".equals(name) || name == null){
            list = infoCommodityService.list(new QueryWrapper<InfoCommodity>().orderByDesc("create_time"));
            return CommonResult.success(list);
        }else {
            list =  infoCommodityService.list(new QueryWrapper<InfoCommodity>().eq("category",name));
            return CommonResult.success(list);
        }
    }

    /**
     * 商品详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public CommonResult detail(@PathVariable("id") Long id){
        InfoCommodity byId = infoCommodityService.getById(id);
        return CommonResult.success(byId);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteCommodity/{id}")
    public CommonResult deleteCommodity(@PathVariable("id") Long id){
        boolean b = infoCommodityService.removeById(id);
        if (b){
            return CommonResult.success("删除成功！");
        }else {
            return CommonResult.failed("删除失败！");
        }
    }

    /**
     * 修改商品属性/新增或取消
     * @return
     */
    @PostMapping("/edit")
    public CommonResult editCommodity(@RequestParam Map<String, Object> commodity){
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(commodity);
        InfoCommodity infoCommodity = jsonObject.toJavaObject(InfoCommodity.class);
        infoCommodityService.saveOrUpdate(infoCommodity);
        return CommonResult.success("成功！");
    }

    /**
     * 热门商品 聚划算  每日好物 列表
     * @param attribute
     * @return
     */
    @GetMapping("/listGoodFood")
    public CommonResult listGoodFood(Integer attribute){
        QueryWrapper<InfoCommodity> infoCommodityQueryWrapper = new QueryWrapper<>();
        if (attribute == 1){
            infoCommodityQueryWrapper.eq("effective_status",1);
            return CommonResult.success(infoCommodityService.list(infoCommodityQueryWrapper));
        }
        if (attribute == 2){
            infoCommodityQueryWrapper.eq("good_status",1);
            return CommonResult.success(infoCommodityService.list(infoCommodityQueryWrapper));
        }
        if (attribute == 3){
            infoCommodityQueryWrapper.eq("hot_status",1);
            return CommonResult.success(infoCommodityService.list(infoCommodityQueryWrapper));
        }
        return CommonResult.failed("系统错误！");
    }
}
